package com.JavaTest6;

import java.util.Random;
import java.util.Scanner;

class Light{
    private boolean is_Error, Switch;
    private int id;
    private static int ID = 1000;

    {
        ID ++ ;
    }

    Light(){
        this.id = ID;
        this.is_Error = false;
        this.Switch = true;
        System.out.println("编号为" + this.id + "号的灯打开");
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void OFF(){
        this.Switch = false;
        System.out.println("编号为" + this.id + "号的灯关闭");
    }

    public void set_Error() throws LightException{
        this.is_Error = true;
        throw new LightException(new String(""+this.id+"号灯损坏"));
    }

    public void Fix_Light(){
        this.is_Error = false;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

class ErrorLights{
    Light[] lights;

    ErrorLights(){}

    ErrorLights(Light[] lights){
        super();
        this.lights = lights;
    }

    public int setErrorLights(int scope) {
        Random r = new Random();

        int rr = r.nextInt(scope);
        try {
            lights[rr].set_Error();
        } catch (LightException e) {
            System.err.println(e.getMessage());
        }

        return rr;
    }
}

class LightException extends Exception{
    LightException(String error){
        super(error);
    }
}

public class tt6 {

    public static void li_Fixed(Light[] l, int[] index){
        for (int j : index) {
            l[j].Fix_Light();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(l[j].getId() + "号灯维修成功");
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("输入灯的数量:");
        int li_Count = sc.nextInt();
        System.out.print("输入灯坏的数量:");
        int er_Count = sc.nextInt();

        Light[] l = new Light[li_Count];
        int[] el = new int[er_Count];

        for(int i = 0; i < li_Count; i ++ ){
            l[i] = new Light();
        }

        ErrorLights eLights = new ErrorLights(l);
        for(int i = 0; i < er_Count; i ++ ){
            el[i] = eLights.setErrorLights(li_Count);
        }


        li_Fixed(l,el);

        for(int i = 0;i < li_Count; i ++ ){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            l[i].OFF();
        }


    }

}



